package ApkTest;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description: 安装知乎应用并启动，执行点击操作<br>
 * createTime: 2017/10/3015:08 <br>
 *
 * @author zzh
 */
public class AppiumZhihuRegister {

    public AndroidDriver driver;

    public void startRecord() throws IOException {
        Runtime rt = Runtime.getRuntime();
        // this code for record the screen of your device
        rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");
    }


    @BeforeClass(alwaysRun = true)
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"FFKDU17322013802");
        capabilities.setCapability(MobileCapabilityType.UDID, "FFKDU17322013802");
//        capabilities.setCapability("deviceName", "FFKDU17322013802");                  //设备名
//        capabilities.setCapability("udid", "FFKDU17322013802");                         //设备id，由adb devices命令查出
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");                          //
        //设置appium给手机发送指令等待执行结果的超时时间，
        // 手机在操作模拟器时时间较长，如执行打开app命令就用了21秒，超过了默认的20秒  appWaitDuration
//        capabilities.setCapability("appWaitDuration", 30000);
//        capabilities.setCapability("deviceReadyTimeout", 120);
//        capabilities.setCapability("androidDeviceReadyTimeout", 120);
//        capabilities.setCapability("avdReadyTimeout", 120000);
//        capabilities.setCapability("avdLaunchTimeout", 120000);
//        capabilities.setCapability("autoWebviewTimeout", 120000);
//        capabilities.setCapability("launchTimeout", 120000);
//        capabilities.setCapability("newCommandTimeout", 120);
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");                           //平台名称
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.0");                            //平台版本
        //配置测试apk app信息，由aapt dump badging xxx.apk命令可查出
//        capabilities.setCapability("appPackage", "com.zhihu.android");
//        capabilities.setCapability("appActivity", ".app.ui.activity.MainActivity ");
        capabilities.setCapability("noSign", true);              //不需要签名
        capabilities.setCapability(MobileCapabilityType.NO_RESET, true);            //不用每次重装apk
//        capabilities.setCapability("sessionOverride", true);    //每次启动时覆盖session，否则第二次后运行会报错不能新建session
        capabilities.setCapability("unicodeKeyboard", true);    //设置键盘
        capabilities.setCapability("resetKeyboard", false);     //设置默认键盘为appium的键盘
        File classpathRoot = new File(System.getProperty("user.dir") + "/androidApp/");
        File app = new File(classpathRoot, "zhihu.apk");
        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());  //设置要装的apk路径
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);      //设置appium等待命令的超时时间
//        startRecord();
    }


    @Test
    public void loginWithMicroBlog() throws InterruptedException {
        List<WebElement> elements = driver.findElements(By.id("com.zhihu.android:id/tab_title"));
        System.out.println(elements.size());
        elements.get(3).click();
        TimeUnit.SECONDS.sleep(2);

        elements.get(4).click();
        driver.closeApp();

    }


    @AfterClass(alwaysRun = true)
    public void tearDown() throws Exception {
        driver.quit();
    }
}
